package com.pac.hospital.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.page.Page;
import com.common.page.PageContext;
import com.common.tools.TimeTools;
import com.common.util.LogicException;
import com.pac.hospital.bean.HospitalBean;
import com.pac.hospital.service.HospitalService;
import com.pac.location.bean.LocationBean;
import com.pac.location.service.LocationService;

@Controller
@Scope("prototype")
@RequestMapping("/hospital")
public class HospitalController {

	@Autowired
	private HospitalService hospitalService;

	@Autowired
	private LocationService locationService;

	/**
	 * 角色列表查询
	 * 
	 * @param userBean
	 * @param page
	 * @param rows
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Page queryUserList(HttpServletRequest request,
			HospitalBean hospitalBean, int page, int rows) {
		// 设置每页记录数
		PageContext.getContext().setPageSize(rows);
		// 跳转到指定页
		PageContext.getContext().gotoPage(page);

		List<HospitalBean> hospitalBeanList = hospitalService
				.queryHospital(hospitalBean);

		Page currentPage = new Page();
		// 总行数
		int totlaRols = PageContext.getContext().getTotal();
		currentPage.setTotal(totlaRols);
		currentPage.setRows(hospitalBeanList);

		return currentPage;
	}

	/***
	 * 页面跳转 <功能详细描述>
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("gotoPage")
	public ModelAndView gotoPage(String pageName) {
		ModelAndView mov = new ModelAndView();
		mov.setViewName("hospital/" + pageName);
		return mov;
	}

	/***
	 * 页面跳转/进入添加医院界面 <功能详细描述>
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("add")
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView mov = new ModelAndView();
		mov.setViewName("hospital/hospitalAdd");
		mov.addObject("status", "add");
		return mov;

	}

	/***
	 * 跳转修改页面 <功能详细描述>
	 * 
	 * @param request
	 * @param hospitalBean
	 * @param id
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("update")
	public ModelAndView update(HttpServletRequest request,
			HospitalBean hospitalBean, String id) {
		List<HospitalBean> hospitalBeanList = hospitalService
				.queryHospital(hospitalBean);
		HospitalBean hospital = hospitalBeanList.get(0);
		// 获取地区id

		LocationBean locationBean = new LocationBean();
		locationBean.setId(hospital.getLocationId());
		List<LocationBean> locationList = locationService
				.queryLocation(locationBean);
		LocationBean location = locationList.get(0);

		// 获取地区省级ID
		request.setAttribute("hospital", hospital);
		ModelAndView mov = new ModelAndView();
		// request.setAttribute("locationId", location.getParentId());
		mov.addObject("locationId", location.getParentId());
		mov.addObject("status", "edit");
		mov.setViewName("hospital/hospitalAdd");
		return mov;

	}

	/***
	 * 新增医院 <功能详细描述>
	 * 
	 * @param hospitalBean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(HospitalBean hospitalBean) {
		boolean result = true;
		String msg = "";
		hospitalBean.setCreateDate(TimeTools.now());

		String id = hospitalService.addHospital(hospitalBean);
		msg = "新增用户【" + hospitalBean.getName() + "】成功";

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("msg", msg);
		map.put("id", id);
		return map;
	}

	/***
	 * 修改医院信息 <功能详细描述>
	 * 
	 * @param hospitalBean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(HospitalBean hospitalBean) {
		boolean result = true;
		String msg = "";
		hospitalService.updateHospital(hospitalBean);
		msg = "修改用户【" + hospitalBean.getName() + "】成功";

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("msg", msg);

		return map;
	}

	/****
	 * 删除医院 <功能详细描述>
	 * 
	 * @param id
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(String id) {
		boolean result = true;
		String msg = "";
		try {
			this.hospitalService.delHospitalById(id);
			msg = "删除用户成功";
		} catch (LogicException e) {
			result = false;
			msg = e.getMessage();
		} catch (Exception e) {
			result = false;
			msg = "删除用户出错，系统内部错误";
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("msg", msg);

		return map;

	}

	/***
	 * 查询所有医院 
	 * 
	 * @param doctorBean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/grade/next")
	@ResponseBody
	public List<HospitalBean> queryNextGradeLocation(HospitalBean hospitalBean) {

		return hospitalService.queryHospital(hospitalBean);
	}
}
